(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.category.cateController', cateController);
	cateController.$inject = ['$scope', 'categoryService'];
	function cateController($scope, categoryService) {
		var cateInfo = $scope.$stateParams.params;
		$scope.limitTreeApi = {};
		$scope.limitData = [];
		$scope.limitSelected = function(limit) {
			if (limit.type == 'ROOT') {
				$scope.go('cda.category.cate.cateList', limit);
			} else if (limit.type == 'CATEGORY') {
				$scope.go('cda.category.cate.cateView', limit);
			} else if (limit.type == 'NODE') {
				$scope.go('cda.category.cate.nodeView', limit);
			} else {
				$scope.go('cda.category.cate.cateList', limit);
			}
		};
		
		$scope.limitTreeApi.init = function(){
			categoryService.getCategoryTree().then(function(response){
				$scope.limitData = response.data || [];
			});
			if(!cateInfo.taskId){
				$scope.limitSelected({'type' : 'ROOT'});
			}
		};
		return $scope.addLimit = function() {
			var selectedLimit = $scope.limitTreeApi.get_selected_branch();
			return $scope.limitTreeApi.add_branch(selectedLimit, {
				label : 'New Limit',
				data : {
					category : selectedLimit.data.category,
					description : 'limit description'
				}
			});
		};
	};
})(angular);